/* Copyright (c) 2011 Stephan Zehrer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import <CoreData/CoreData.h>

// XMLStore is a abstract (super)class providing the general part of an Atomic Store implementation
// Subclasses have to implement the backend implementation for XML read and write operations.
// Know Subclasses are:
//   - XMLDocumentStore
//   - TODO :)
@interface XMLStore : NSAtomicStore {
    NSXMLDocument *_document; // TODO move to XMLDocumentStore

    NSMutableDictionary *refDataToCacheNodeMap;
    NSMutableDictionary *refDataToXMLElement;
	
	NSInteger nextID;  // or use a shorten UUID instead of a counter
	NSEntityDescription *_rootEntityType;
}

@property (retain) NSXMLDocument *document;
@property (retain) NSEntityDescription *rootEntityType;

// Init & Remove Methods
- (id) initWithPersistentStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator configurationName:(NSString *)configurationName URL:(NSURL *)url options:(NSDictionary *)options;
- (void) willRemoveFromPersistentStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator;

// Load & Save Methods
- (BOOL) load:(NSError **)error;
- (BOOL) save:(NSError **)error;

// New, Update & Remove Cache Nodes
- (void) updateCacheNode:(NSAtomicStoreCacheNode *)node fromManagedObject:(NSManagedObject *)managedObject;
- (void) willRemoveCacheNodes:(NSSet *)cacheNodes;

// 
- (id) newReferenceObjectForManagedObject:(NSManagedObject *)managedObject;


// additional methodes
- (id) cacheNodeForEntity:(NSEntityDescription *)entity withReferenceData:(id)refID;
- (id) loadEntity:(NSXMLElement*) element withID:(NSString*) refID;

// TODO: define protocol
- (void) readMetadata;
- (void) writeMetadata;

- (NSXMLElement *) parentForObject:(NSManagedObject *)managedObject;
- (NSXMLElement *) newElementForEntity:(NSEntityDescription *)entity;

//- (NSXMLElement *)elementFromManagedObject:(NSManagedObject *)managedObject;

- (void) updateAttributesInCacheNode:(NSAtomicStoreCacheNode *)node andElement:(NSXMLElement *)element fromManagedObject:(NSManagedObject *)managedObject;
- (void) updateRelationshipsInCacheNode:(NSAtomicStoreCacheNode *)node andElement:(NSXMLElement *)element fromManagedObject:(NSManagedObject *)managedObject;


@end
